package com.xhh.nphm.api.controller;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.xhh.nphm.api.util.ListPageDsCntnr;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

@Component
public class ForListPage
{
  static final Log logger = LogFactory.getLog(ForListPage.class);

  @Resource
  private ConDatabaseBySqlServer conDatabaseBySqlServer;

  private List<Map<String, String>> buildPageListDS(ResultSet rs)
  {
    List result = new ArrayList();
    try {
      int column = rs.getMetaData().getColumnCount();
      String[] names = new String[column];
      for (int i = 1; i <= column; i++) {
        names[(i - 1)] = rs.getMetaData().getColumnName(i);
      }
      while (rs.next()) {
        Map rowData = new HashMap();
        for (String name : names) {
          rowData.put(name, rs.getString(name));
        }
        result.add(rowData);
      }
    } catch (SQLException e) {
      logger.error("call jdbc error: " + e.getMessage());
    }

    return result;
  }

  public ListPageDsCntnr procedureQuery(String pageNum, String pageSize, String fieldsName, String tabName, String pkName, String strWhere, String strOrd)
    throws SQLException
  {
	String sqlStr = "select "+fieldsName+" from "+tabName+" where "+strWhere+" "+strOrd;
	System.out.println(sqlStr);
    Connection con = this.conDatabaseBySqlServer.getConnection();

    CallableStatement call = con
      .prepareCall("{call ProcQueryByPage(?,?,?,?,?,?,?,?)}");
    call.setString(1, pageNum);
    call.setString(2, pageSize);
    call.setString(3, fieldsName);
    call.setString(4, tabName);
    call.setString(5, pkName);
    call.setString(6, strWhere);
    call.setString(7, strOrd);
    call.registerOutParameter(8, 4);
    String sql = call.toString();
    ResultSet rs = null;
    ListPageDsCntnr ds = null;
    try {
      rs = call.executeQuery();
      ds = new ListPageDsCntnr(buildPageListDS(rs), call.getInt(8));
    } catch (Exception e) {
      e.printStackTrace();
      try
      {
        rs.close();
      } catch (Exception localException1) {
      }
      try {
        call.close();
      } catch (Exception localException2) {
      }
      try {
        con.close();
      }
      catch (Exception localException3)
      {
      }
    }
    finally
    {
      try
      {
        rs.close();
      } catch (Exception localException4) {
      }
      try {
        call.close();
      } catch (Exception localException5) {
      }
      try {
        con.close();
      } catch (Exception localException6) {
      }
    }
    return ds;
  }

  public List<Map<String, String>> queryList(String sql, Object[] args)
    throws SQLException
  {
    Connection con = this.conDatabaseBySqlServer.getConnection();

    PreparedStatement ps = con.prepareStatement(sql);
    for (int i = 0; i < args.length; i++) {
      ps.setObject(i + 1, args[i]);
    }
    ResultSet rs = ps.executeQuery();
    List result = buildPageListDS(rs);
    try {
      rs.close();
    }
    catch (Exception localException) {
    }
    try {
      ps.close();
    }
    catch (Exception localException1) {
    }
    try {
      con.close();
    } catch (Exception localException2) {
    }
    return result;
  }
}